Mathematica 2. del

Funkcije višjega reda

Map

Map[f,g[a,b,c,d]]

f/@ g[a,b,c,d]

Sin/@ {a,b,c,d}

f/@ (a+b+c+d)

Map[AtomQ,{2,1/3,0.3333,1+I,a,"a"}]

Map[Head,{2,1/3,0.3333,1+I,a,"a"}]

Map[Length,{2,1/3,0.3333,1+I,a,"a"}]

Map[Length,{{2},{1/3,0.3333},{1+I,a,"a"}}]

f/@1/3

Zgled: število deliteljev

Divisors[6]

Length[%]

steviloDeliteljev[n_]:=Length[Divisors[n]]

steviloDeliteljev[111111]

FactorInteger[111111]

Map[steviloDeliteljev,Range[2010,2020]]

Apply

Apply[f,g[a,b,c,d]]

f@@g[a,b,c,d]

Apply[f,a+b+c+d]

f@@1/3

Plus@@{a,b,c,d}

Total[{a,b,c,d}]

Times@@{a,b,c,d}

Zgled: n!

Range[4]

Times@@%

f[n_]:=Times@@Range[n]

Table[{n,f[n]},{n,0,10}]//TableForm

Table[{n,n!},{n,0,10}]//TableForm

Zgled: popolna števila

Divisors[28]

Drop[%,-1]

Plus@@%

popolnoQ[n_]:=(Plus@@Divisors[n])==2n

popolnoQ[28]

popolnoQ[27]

Select[Range[100],popolnoQ]

Select[Range[1000],popolnoQ]

Select[Range[10000],popolnoQ]

Nest, NestList in FixedPoint

Clear[f,x]

Nest[f,x,5]

NestList[f,x,5]

Zgled: vsota pravih deliteljev

f[n_]:=Plus@@Divisors[n]-n

NestList[f,100,10]

NestList[f,1000,10]

NestList[f,1000,20]

FixedPoint[f,1000]

Table[FixedPoint[f,n],{n,1,100}]

Table[FixedPoint[f,n],{n,101,200}]

Table[FixedPoint[f,n],{n,201,300}]

Table[FixedPoint[f,n],{n,201,250}]

Table[FixedPoint[f,n],{n,201,225}]

Table[FixedPoint[f,n],{n,201,212}]

Table[FixedPoint[f,n],{n,213,219}]

Table[FixedPoint[f,n],{n,220,222}]

FixedPoint[f,220]

NestList[f,220,20]

Table[FixedPoint[f,n],{n,221,275}]

FixedPoint[f,276]

NestList[f,276,10]

NestList[f,276,30]

NestList[f,276,100]

Zgled: reševanje nelinearnih enačb z navadno iteracijo

Solve[Cos[x]==Sin[x]+1,x]

Solve[Cos[x]==x]

Plot[{Cos[x],x},{x,0,Pi/2},AspectRatio->Automatic]

NestList[Cos,1.,40]

FixedPoint[Cos,1.]

Cos[%]

FindRoot[Cos[x]==x,{x,1}]

Fold in FoldList

Fold[f,a0,{a1,a2,a3,a4}]

FoldList[f,a0,{a1,a2,a3}]

FoldList[Plus,0,{a,b,c,d,e}]

FoldList[Times,1,Range[8]]

Zgled: levo-desni maksimumi

FoldList[Max,-Infinity,{2,5,3,4,8,9,7,6,11,10}]

Rest[%]

Union[%]

LevoDesniMaksimumi[s_]:=Union[Rest[FoldList[Max,-Infinity,s]]]

LevoDesniMaksimumi[{2,5,3,4,8,9,7,6,11,10}]

Zgled: Vrednost dvojiškega zapisa

Katero število ima dvojiški zapis 1110101?

f[x_,y_]:=2x+y

Fold[f,0,{(Subscript[c, 6]),(Subscript[c, 5]),(Subscript[c, 4]),(Subscript[c, 3]),(Subscript[c, 2]),(Subscript[c, 1]),(Subscript[c, 0])}]

Expand[%]

Fold[f,0,{1,1,1,0,1,0,1}]

FromDigits[{1,1,1,0,1,0,1},2]

IntegerDigits[117,2]

Čiste funkcije

f[x_]:=x^2+1

f[6]

Clear[f]

f[6]

Function[x,x^2+1][6]

#^2+1& [6]

f[x_,y_]:=x^2+y^2

f[5,6]

Clear[f]

Function[{x,y},x^2+y^2][5,6]

#1^2+#2^2& [5,6]

Zgled: Števila med 1 in 100 z največ delitelji

steviloDeliteljev[n_]:=Length[Divisors[n]]

Max[steviloDeliteljev/@Range[100]]

Select[Range[100],steviloDeliteljev[#]==12&]

Max[steviloDeliteljev/@Range[1000]]

Select[Range[1000],steviloDeliteljev[#]==32&]

Select[Range[1000],steviloDeliteljev[#]==33&]

Select[Range[1000],steviloDeliteljev[#]==31&]

Select[Range[1000],steviloDeliteljev[#]==30&]

Select[Range[1000],steviloDeliteljev[#]==29&]

Select[Range[1000],steviloDeliteljev[#]==28&]

Select[Range[1000],steviloDeliteljev[#]==27&]

Select[Range[1000],steviloDeliteljev[#]==26&]

Select[Range[1000],steviloDeliteljev[#]==25&]

Select[Range[1000],steviloDeliteljev[#]==24&]

Zgled: Popolna števila višjih redov

PopolnoQ[n_,k_]:=(Plus@@Divisors[n])==k n

Select[Range[10000],PopolnoQ[#,2]&]

Select[Range[10000],PopolnoQ[#,3]&]

Plus@@Divisors[672]

%/3

Select[Range[10000],PopolnoQ[#,4]&]

Zgled: Vrednost b-iškega zapisa

vrednostZapisa[s_,b_]:=Fold[#1 b+#2&,0,s]

vrednostZapisa[{1,1,1,0,1,0,1},2]

vrednostZapisa[{1,1,1,0,1,0,1},3]

vrednostZapisa[{1,1,1,0,1,0,1},10]

vrednostZapisa[{1,1,1,0,1,0,1},-2]

IntegerDigits[#,2]&/@Range[0,31]//TableForm

vrednostZapisa[#,-2]&/@%

%//Union

%//Length

Takojšnje in zakasnjeno prirejanje

Clear[a,x,y]

a=3

x=a

y:=a

x

y

a=7

x

y

?x

?y

Clear[x,y]

(x+y+z)^6

(x+y+z)^6//Expand

%//Length

Clear[f,g]

f[p_]=Length[Expand[p]];

g[p_]:=Length[Expand[p]];

f[(x+y+z)^6]

g[(x+y+z)^6]

?f

Expand[p]

Length[%]

?g